package Marks.Webshop.Database.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.sql.PreparedStatement;

import Marks.Webshop.Database.dto.ProductDto;

public class ProductDao extends AbstractDao<ProductDto> {

	public ProductDao(Connection conn) {
		super(conn);
	}

	@Override
	public Integer save(ProductDto dto) {
		try {
			PreparedStatement pstmt = conn.prepareStatement("INSERT INTO products ");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void update(ProductDto dto) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(Integer pk) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public ProductDto findById(Integer pk) {
		try {
			PreparedStatement pstmt = this.conn.prepareStatement("SELECT * FROM products WHERE cikkszam = ?");
			pstmt.setInt(1, pk);
			
			ResultSet rs = pstmt.executeQuery();
			
			if (rs.next()){
				return new ProductDto(rs.getInt(1), rs.getString(2), rs.getInt(3),
						rs.getString(4), rs.getString(5), rs.getInt(6));
			}else{
				return null;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

	@Override
	public List<ProductDto> findAll() {
		try {
			PreparedStatement pstmt = this.conn.prepareStatement("SELECT * FROM products");
			ResultSet rs = pstmt.executeQuery();
			
			if(rs.next()){
				List<ProductDto> list = new ArrayList<ProductDto>();
				do{
					list.add(new ProductDto(rs.getInt(1), rs.getString(2), rs.getInt(3),
						rs.getString(4), rs.getString(5), rs.getInt(6)));
				}while(rs.next());
				return list;
				
			}else{
				return null;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public List<ProductDto> findAllLimit(int from, int to) {
		try {
			PreparedStatement pstmt = this.conn.prepareStatement("SELECT * FROM products LIMIT ?, ?");
			pstmt.setInt(1, from);
			pstmt.setInt(2, to);
			
			ResultSet rs = pstmt.executeQuery();
			
			if(rs.next()){
				List<ProductDto> list = new ArrayList<ProductDto>();
				do{
					list.add(new ProductDto(rs.getInt(1), rs.getString(2), rs.getInt(3),
							rs.getString(4), rs.getString(5), rs.getInt(6)));
				}while(rs.next());
				return list;
			}else{
				return null;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
